//
//  ChangePersonalInfoViewController.m
//  CleanerAir_Project
//
//  Created by 付亚杰 on 16/6/23.
//  Copyright © 2016年 Beijing Colin El Internet. All rights reserved.
//

#import "ChangePersonalInfoViewController.h"
#import "NSDate+Helper.h"
#import "NSString+check.h"

#define KFontSize 14
#define KTextFiledH 40
#define KTextViewH 100

#define KViewBlackColorValue 240
#define KLineColorValue 190

#define KViewBlackColor [UIColor colorWithRed:KViewBlackColorValue/255.0 green:KViewBlackColorValue/255.0 blue:KViewBlackColorValue/255.0 alpha:1]


#define KLineColor [UIColor colorWithRed:KLineColorValue/255.0 green:KLineColorValue/255.0 blue:KLineColorValue/255.0 alpha:1]

@interface ChangePersonalInfoViewController ()<UITextFieldDelegate,UITextViewDelegate,UIImagePickerControllerDelegate>

@property (nonatomic, strong) UITextField *textFile;

@property (nonatomic, strong) UILabel *label;

@property (nonatomic, strong) UITextView *textView;

@property (nonatomic, strong) UILabel *instructions;

@end

@implementation ChangePersonalInfoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"修改个人信息";
    
    UIButton *rightButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 15, 47, 17)];
    [rightButton setTitleColor:[UIColor whiteColor] forState:(UIControlStateNormal)];
    [rightButton setTitle:@"保存" forState:(UIControlStateNormal)];
    [rightButton addTarget:self action:@selector(save) forControlEvents:UIControlEventTouchUpInside];
    //    [leftButton setBackgroundImage:[UIImage imageNamed:@"navbtn.png"] forState:UIControlStateNormal];
    UIBarButtonItem *rightBarBtn = [[UIBarButtonItem alloc] initWithCustomView:rightButton];
    self.navigationItem.rightBarButtonItem = rightBarBtn;
    
    self.view.backgroundColor = KViewBlackColor;
    
    [self initSubView];
    
    // Do any additional setup after loading the view.
}

#pragma mark -- 保存
- (void)save{
    
    switch (self.changeType) {
        case NICK_NAME:{
            
            if ([NSString isNumLetterAndChinese:self.textFile.text showMessage:@"昵称只能由数字、字母、汉字组成"]) {
                return;
            }
            
            self.info = self.textFile.text;
            
            break;
        }
        case SEX:{
            
            if ([self.label.text isEqualToString:@""]) {
                [ShowMessage showCenterViewMessage:@"请选择性别"];
                return;
            }
            
            self.info = self.label.text;
            
            break;
        }
        case BRITHDAY:{
            
            if([self.label.text isEqualToString:@""]){
                
                [ShowMessage showCenterViewMessage:@"请选择生日"];
                return;
            }
            
            self.info = self.label.text;
            
            break;
        }
        case EMAIL:{
            
            if ([NSString isMail:self.textFile.text]) {
                return;
            }
            
            self.info = self.textFile.text;
            
            break;
        }
        case ADDRESS:{
            
            if ([NSString isNumLetterAndChinese:self.textView.text showMessage:@"昵称只能由数字、字母、汉字组成"]) {
                return;
            }
            
            if (self.textView.text.length > 30) {
                [ShowMessage showCenterViewMessage:@"地址不能超过30个字"];
                return;
            }
            
            self.info = self.textView.text;
            
            break;
        }
        case INTRODUCTION:{
            if (self.textView.text.length > 40) {
                [ShowMessage showCenterViewMessage:@"个人说明不能超过30个字"];
                return;
            }
            
            self.info = self.textView.text;
            
            break;
        }
            
        default:
            break;
    }

    
    if (self.changePerInfoBlock != nil) {
        
        self.changePerInfoBlock(self.info);
    }
    
}

#pragma mark -- 选择生日
- (void)dateClick{
    
    [self.view endEditing:YES];
    
    UIDatePicker *picker = [[UIDatePicker alloc]init];
    picker.datePickerMode = UIDatePickerModeDate;
    
    picker.frame = CGRectMake(0, 40, MAIN_WIDTH, 200);
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"请选择\n\n\n\n\n\n\n\n\n\n\n\n" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
        NSDate *date = picker.date;
        
        self.label.text = [date stringWithFormat:@"yyyy-MM-dd"];;
        
    }];
    [alertController.view addSubview:picker];
    [alertController addAction:cancelAction];
    [self presentViewController:alertController animated:YES completion:nil];
    
}

#pragma mark -- 修改性别
- (void)changeSex{
    
    
    UIAlertController *alterController = [UIAlertController alertControllerWithTitle:@"提示" message:@"请选择性别" preferredStyle:(UIAlertControllerStyleActionSheet)];
    
    UIAlertAction *cameralAction = [UIAlertAction actionWithTitle:@"男" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
        
        self.label.text = @"男";
    }];
    
    UIAlertAction *photoAction =[UIAlertAction actionWithTitle:@"女" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
        
        self.label.text = @"女";
    }];
    
    UIAlertAction *cancelAction =[UIAlertAction actionWithTitle:@"保密" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
        
        self.label.text = @"保密";
    }];
    
    [alterController addAction:cancelAction];
    [alterController addAction:cameralAction];
    [alterController addAction:photoAction];
    
    
    [self presentViewController:alterController animated:YES completion:nil];
}


#pragma mark -- 初始化控件

- (void)initSubView{
    
    
    UIView *aview = [[UIView alloc] initWithFrame:(CGRectMake(0,50, MAIN_WIDTH, 0.5))];
    aview.backgroundColor = KLineColor;
    [self.view addSubview:aview];
    
    UIView *downView;
    
    self.instructions = [[UILabel alloc] init];
    self.instructions.font = [UIFont systemFontOfSize:KFontSize - 2];
    self.instructions.textColor = [UIColor grayColor];
    self.instructions.textAlignment = NSTextAlignmentCenter;
    
    switch (self.changeType) {
        case NICK_NAME:{
            
            self.textFile = [[UITextField alloc] initWithFrame:(CGRectMake(0, aview.y + aview.height, MAIN_WIDTH, KTextFiledH))];
            self.textFile.font = [UIFont systemFontOfSize:KFontSize];
            self.textFile.textColor = [UIColor grayColor];
            self.textFile.textAlignment = NSTextAlignmentCenter;
            self.textFile.text = self.info;
            self.textFile.backgroundColor = [UIColor whiteColor];
            self.textFile.borderStyle = UITextBorderStyleNone;
            self.textFile.delegate = self;
            
            self.instructions.text = @"昵称只能由汉字、字母、数字组成";
            
            downView = [[UIView alloc] initWithFrame:(CGRectMake(0, CGRectGetMaxY(self.textFile.frame), MAIN_WIDTH, 0.5))];
            
            
            [self.view addSubview:self.textFile];
            [self.view addSubview:downView];
            
            break;
        }
        case SEX:{
            
            self.label = [[UILabel alloc] initWithFrame:(CGRectMake(0, aview.y + aview.height, MAIN_WIDTH, KTextFiledH))];
            self.label.textAlignment = NSTextAlignmentCenter;
            self.label.textColor = [UIColor grayColor];
            self.label.font = [UIFont systemFontOfSize:KFontSize];
            self.label.backgroundColor = [UIColor whiteColor];
            self.instructions.text = @"点击修改性别";
            self.label.text = self.info;
            self.label.userInteractionEnabled = YES;
            [self.label addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(changeSex)]];
            
            downView = [[UIView alloc] initWithFrame:(CGRectMake(0, CGRectGetMaxY(self.label.frame), MAIN_WIDTH, 0.5))];
           
            [self.view addSubview:self.label];
            [self.view addSubview:downView];
            
            break;
        }
        case BRITHDAY:{
            
            self.label = [[UILabel alloc] initWithFrame:(CGRectMake(0, aview.y + aview.height, MAIN_WIDTH, KTextFiledH))];
            self.label.textAlignment = NSTextAlignmentCenter;
            self.label.textColor = [UIColor grayColor];
            self.label.font = [UIFont systemFontOfSize:KFontSize];
            self.label.backgroundColor = [UIColor whiteColor];
            self.label.text = self.info;
            self.label.userInteractionEnabled = YES;
            [self.label addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dateClick)]];
            
            self.instructions.text = @"点击修改生日";
            
            downView = [[UIView alloc] initWithFrame:(CGRectMake(0, CGRectGetMaxY(self.label.frame), MAIN_WIDTH, 0.5))];
            
            [self.view addSubview:self.label];
            [self.view addSubview:downView];
            
            break;
        }
        case EMAIL:{
            
            self.textFile = [[UITextField alloc] initWithFrame:(CGRectMake(0, aview.y + aview.height, MAIN_WIDTH, KTextFiledH))];
            self.textFile.font = [UIFont systemFontOfSize:KFontSize];
            self.textFile.textColor = [UIColor grayColor];
            self.textFile.backgroundColor = [UIColor whiteColor];
            self.textFile.textAlignment = NSTextAlignmentCenter;
            self.textFile.text = self.info;
            self.textFile.delegate = self;
            self.instructions.text = @"请填写邮箱";
            
            downView = [[UIView alloc] initWithFrame:(CGRectMake(0, CGRectGetMaxY(self.textFile.frame), MAIN_WIDTH, 0.5))];
            
            [self.view addSubview:self.textFile];
            [self.view addSubview:downView];
            
            break;
        }
        case ADDRESS:{
            
            self.textView = [[UITextView alloc] initWithFrame:(CGRectMake(0, aview.y + aview.height, MAIN_WIDTH, 80))];
            self.textView.font = [UIFont systemFontOfSize:KFontSize];
//            self.textView.textColor = [UIColor grayColor];
            self.textView.textAlignment = NSTextAlignmentCenter;
            self.textView.backgroundColor = [UIColor whiteColor];
            self.textView.text = self.info;
            self.textView.delegate = self;
            
            self.instructions.text = @"地址不能超过30个汉字";
            
            downView = [[UIView alloc] initWithFrame:(CGRectMake(0, CGRectGetMaxY(self.textView.frame), MAIN_WIDTH, 0.5))];
            
            [self.view addSubview:self.textView];
            [self.view addSubview:downView];
            
            break;
        }
        case INTRODUCTION:{
            
            self.textView = [[UITextView alloc] initWithFrame:(CGRectMake(0, aview.y + aview.height, MAIN_WIDTH, 80))];
            self.textView.font = [UIFont systemFontOfSize:KFontSize];
//            self.textView.textColor = [UIColor grayColor];
            self.textView.textAlignment = NSTextAlignmentCenter;
            self.textView.text = self.info;
            self.textView.delegate = self;
            self.textView.backgroundColor = [UIColor whiteColor];
            
            self.instructions.text = @"个人说明不能超过40个汉字";
            
            downView = [[UIView alloc] initWithFrame:(CGRectMake(0, CGRectGetMaxY(self.textView.frame), MAIN_WIDTH, 0.5))];
            
            [self.view addSubview:self.textView];
            [self.view addSubview:downView];
            
            break;
        }
            
        default:
            break;
    }
    
    
    
    downView.backgroundColor = KLineColor;
    self.instructions.frame = CGRectMake(0, CGRectGetMaxY(downView.frame) , MAIN_WIDTH, 30);
    [self.view addSubview:self.instructions];
    
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    [self.view endEditing:YES];
    return YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
